﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Print
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {

            Font titleFont = new Font("宋体", 9, FontStyle.Bold);//标题字体            

            Font fntTxt = new Font("宋体", 9, FontStyle.Regular);//正文文字            

            Brush brush = new SolidBrush(Color.Black);//画刷            

            Pen pen = new Pen(Color.Black);           //线条颜色            

            Point po = new Point(10, 10);

            try
            {

                e.Graphics.DrawString(GetPrintSW().ToString(), titleFont, brush, po);   //DrawString方式进行打印。         

            }

            catch (Exception ex)
            {

                MessageBox.Show(this, "打印出错！\r\n"+ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        ///GetPrintSw方法用来构造打印文本，内部StringBuilder.AppendLine在Drawstring时单独占有一行。

        public StringBuilder GetPrintSW()
        {

            StringBuilder sb = new StringBuilder();

            string tou = "测试管理公司名称";

            string address = "河南洛阳";

            string saleID = "2010930233330";    //单号        

            string item = "项目";

            decimal price = 25.00M;

            int count = 5;

            decimal total = 0.00M;

            decimal fukuan = 500.00M;

            sb.AppendLine(" " + tou + " \n");

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("日期:" + DateTime.Now.ToShortDateString() + " " + "单号:" + saleID);

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("项目" + "      " + "数量" + "    " + "单价" + "    " + "小计");

            for (int i = 0; i < count; i++)
            {

                decimal xiaoji = (i + 1) * price;

                sb.AppendLine(item + (i + 1) + "      " + (i + 1) + "     " + price + "    " + xiaoji);

                total += xiaoji;

            }

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("数量:" + count + "  合计: " + total);

            sb.AppendLine("付款:" + fukuan);

            sb.AppendLine("现金找零:" + (fukuan - total));

            sb.AppendLine("-----------------------------------------");

            sb.AppendLine("地址:" + address + "");

            sb.AppendLine("电话:123456789 123456789");

            sb.AppendLine("谢谢惠顾欢迎下次光临 ");

            sb.AppendLine("-----------------------------------------");

            return sb;

        }



        //在打印页面创建PrintDocument对象 

        PrintDocument pd = new PrintDocument();

        //如果需要打印预览，可以声明打印预览变量，预览这一部分，这里不再介绍，博客园里相应代码多到没边，可以自行查询。

        //打印按钮事件：

        private void btnPrint_Click(object sender, EventArgs e)
        {
           //测试数据
            DataTable dataTable =new DataTable();
            dataTable.Columns.Add("id", typeof(string));
            dataTable.Columns.Add("value1", typeof(double));
            dataTable.Columns.Add("value2", typeof(double));
            for (int i =0; i <45; i++)
            {
                dataTable.Rows.Add(new object[] {"北京市-"+i.ToString(),100*i,234.345*i});
            }
            string[] bottomStr={"操作人员：Ryan","打印日期："+DateTime.Now.ToShortDateString(),"审核人员：","财务人员："};
            string[] header = {"城市名称","预定数量","平均价格" };
            DataReprint dr = new DataReprint("2010年12月12日-2011年12月12日城市预订分布统计报表", "2010年12月12日-2011年12月12日城市预订分布统计图", dataTable, header, bottomStr);
            PrintPreviewDialog p = dr.PrintReprint();
           // this.groupBox1.Controls.Add(p);           
            p.Show();
          //  groupBox1.Width +=1;
          //  this.Refresh();    
        }

        private void button2_Click(object sender, EventArgs e)
        {
            pd.PrintPage += new PrintPageEventHandler(printDocument_PrintPage); //打印页面需指定相应的PrintDocument_PrintPrintPage事件委托  
            printPreviewDialog1.Document = pd;
            printPreviewDialog1.ShowIcon = false;
            printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            //printPreviewDialog1.TopLevel = false;
            printPreviewDialog1.Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //测试数据
            DataTable dataTable = new DataTable();
            dataTable.Columns.Add("id", typeof(string));
            dataTable.Columns.Add("value1", typeof(double));
            dataTable.Columns.Add("value2", typeof(double));
            for (int i = 0; i < 5; i++)
            {
                dataTable.Rows.Add(new object[] { "北京市-" + i.ToString(), 100 * i, 234.345 * i });
            }
            //string[] bottomStr = { "注：凡超出本委托（授权）范围的行为，由其本人承担全部法律责任，与本公司无关。", "打印日期：" + DateTime.Now.ToShortDateString(), "操作员：" };
            string[] bottomStr = { "注：凡超出本委托（授权）范围的行为，由其本人承担全部法律责任，与本公司无关。操作员：赵美芳" };
            string[] header = { "城市名称", "预定数量", "平均价格" };
            //DataReprint(string title, string licname,string pname,string idcard,string licinfo,string licstartdate,string licenddate, string[] bottomStr)
            DataReprint dr = new DataReprint("法 人 授 权 委 托 书", "长春万德制药有限公司","王萍", "513826198704241424", "销售业务","2020-09-24","2021-09-23", bottomStr);
            printPreviewDialog1.Document = dr.PrintReDocument();
            printPreviewDialog1.PrintPreviewControl.Zoom = 1.0;
            printPreviewDialog1.TopLevel = false;
            dr.SetPrintPreviewDialog(printPreviewDialog1);

            this.groupBox1.Controls.Add(printPreviewDialog1);    
            printPreviewDialog1.Show();
            groupBox1.Width += 1;
            this.Refresh();  
        }
    }
}
